package com.weu.contorller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weu.bean.Blog;
import com.weu.bean.Comment;
import com.weu.service.BlogService;
import com.weu.service.CommentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class BlogController {
    @Resource
    private BlogService blogService;
    @Resource
    private CommentService commentService;

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @GetMapping("/blogs")
    public String findPage(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "6") Integer pageSize,
                           Model model) {
        //为了程序的严谨性，判断非空：
        if (pageNum == null) {
            pageNum = 1;   //设置默认当前页
        }
        if (pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 6;    //设置默认每页显示的数据数
        }
        System.out.println("当前页是：" + pageNum + "显示条数是：" + pageSize);

        //1.引入分页插件,pageNum是第几页，pageSize是每页显示多少条,默认查询总数count
        PageHelper.startPage(pageNum, pageSize);
        //2.紧跟的查询就是一个分页查询-必须紧跟.后面的其他查询不会被分页，除非再次调用PageHelper.startPage
        try {
            List<Blog> blogList = blogService.getBlogs();
            System.out.println("分页数据：" + blogList);
            //3.使用PageInfo包装查询后的结果,5是连续显示的条数,结果list类型是Page<E>
            PageInfo<Blog> pageInfo = new PageInfo<Blog>(blogList, pageSize);
            //4.使用model/map/modelandview等带回前端
            model.addAttribute("pageInfo", pageInfo);
        } finally {
            //清理 ThreadLocal 存储的分页参数,保证线程安全
            PageHelper.clearPage();
        }
        //5.设置返回的jsp/html等前端页面
        // thymeleaf默认就会拼串classpath:/templates/xxxx.html
        return "blog-grid";
    }


    /**
     * 来到个人博客页面
     *
     * @param pageNum
     * @param pageSize
     * @param blogid
     * @param model
     * @return
     */
    @GetMapping("/blogs/{blogid}")
    public String toSingleBlogPage(@PathVariable(value = "blogid") int blogid,
                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize,
                                   Model model) {
        //为了程序的严谨性，判断非空：
        if (pageNum == null) {
            pageNum = 1;   //设置默认当前页
        }
        if (pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 3;    //设置默认每页显示的数据数
        }
        System.out.println("当前页是：" + pageNum + "显示条数是：" + pageSize);

        //1.引入分页插件,pageNum是第几页，pageSize是每页显示多少条,默认查询总数count
        PageHelper.startPage(pageNum, pageSize);
        //2.紧跟的查询就是一个分页查询-必须紧跟.后面的其他查询不会被分页，除非再次调用PageHelper.startPage
        try {
            Blog blog = blogService.getBlog(blogid);
            model.addAttribute("blog", blog);
            List<Comment> commentList = commentService.getComments(blogid);
            System.out.println("分页数据：" + commentList);
            //3.使用PageInfo包装查询后的结果,5是连续显示的条数,结果list类型是Page<E>
            PageInfo<Comment> pageInfo = new PageInfo<Comment>(commentList, pageSize);
            //4.使用model/map/modelandview等带回前端
            model.addAttribute("pageInfo", pageInfo);
        } finally {
            //清理 ThreadLocal 存储的分页参数,保证线程安全
            PageHelper.clearPage();
        }
        //5.设置返回的jsp/html等前端页面
        // thymeleaf默认就会拼串classpath:/templates/xxxx.html
        return "single-blog";
    }

    /**
     * 添加评论
     * @param comment
     * @param
     * @return
     */
    @PostMapping("/blogs/{blogid}")
    public String insertComment(Comment comment) {
        System.out.println("新增的评论：" + comment);
        commentService.insertComment(comment);
        //  添加成功，重定向到查询页面
        return "redirect:/blogs/{blogid}";
    }

}
